﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Set.aspx.cs" Inherits="Lottery.Set" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />

    <title>电器(东莞)有限公司2018迎春晚会-设置</title>

    <%--基础js--%>
    <script type="text/javascript" src="./Scripts/EasyUI/jquery.min.js"></script>
    <%--easyui 的js--%>
    <script type="text/javascript" src="./Scripts/EasyUI/jquery.easyui.min.js" charset="utf-8"></script>
    <%--中文js--%>
    <script type="text/javascript" src="./Scripts/EasyUI/locale/easyui-lang-zh_CN.js"></script>
    <%--基础样式--%>
    <link rel="stylesheet" href="./Content/EasyUI/themes/metro/easyui.css" />
    <%--图标样式--%>
    <link rel="stylesheet" href="./Content/EasyUI/themes/icon.css" />

    <%--基础js--%>
    <script type="text/javascript" src="./Scripts/bootstrap.min.js"></script>
    <link rel="stylesheet" href="./Content/bootstrap/bootstrap.css" />

    <script type="text/javascript">
       var editIndex = undefined;
       function endEditing() {
           if (editIndex == undefined) { return true }
           if ($('#dg').datagrid('validateRow', editIndex)) {
               $('#dg').datagrid('endEdit', editIndex);
               editIndex = undefined;
               return true;
           } else {
               return false;
           }
       }
       function onClickRow(index) {
           if (editIndex != index) {
               if (endEditing()) {
                   $('#dg').datagrid('selectRow', index)
                           .datagrid('beginEdit', index);
                   editIndex = index;
               } else {
                   $('#dg').datagrid('selectRow', editIndex);
               }
           }
       }
       function append() {
           if (endEditing()) {
               $('#dg').datagrid('appendRow', { Name: '' });
               editIndex = $('#dg').datagrid('getRows').length - 1;
               $('#dg').datagrid('selectRow', editIndex)
                       .datagrid('beginEdit', editIndex);
           }
       }
       function removeit() {
           if (editIndex == undefined) { return }
           $('#dg').datagrid('cancelEdit', editIndex)
                   .datagrid('deleteRow', editIndex);
           editIndex = undefined;
       }
       function accept() {
           if (endEditing()) {
               var table = $("#dg").datagrid("getRows");
               var obj = "[";
               for (var i = 0; i < table.length; i++) {
                   $('#dg').datagrid("endEdit", i);
                   obj += JSON.stringify(table[i]);
                   if (i != table.length - 1) { obj += ","; }
               }
               obj += "]";

               $.ajax({
                   type: "POST",
                   url: " ./Handler/SetHandler.ashx?action=Save",
                   processData: true,//当发送字符串时设为true,当发送文档对象时设为false
                   data: { dg: obj },
                   async: false,
                   timeout: 5000
               });
           }
           alert("保存成功！\n Saved Successfully !");
           window.location.reload();
       }

       function reject() {
           $('#dg').datagrid('rejectChanges');
           editIndex = undefined;
       }

       function Submit() {
           $.ajax({
               type: "POST",
               url: " ./Handler/SetHandler.ashx?action=Submit",
               processData: true,//当发送字符串时设为true,当发送文档对象时设为false
               data: {
                   LType: $('#LType').combobox('getValue'),
                   LTypeName: $('#LType').combobox('getText'),
                   CardId: $('#Employee').combobox('getValue'),
                   Employee: $('#Employee').combobox('getText')
               },
               async: false,
               timeout: 5000
           });
           alert("提交成功！\n Submit Successfully !");
           window.location.reload();
       }


       function Delete(LType, CardId) {
           if (confirm('确定要删除' + CardId + '的中奖信息么？')) {
               $.ajax({
                   type: "POST",
                   url: " ./Handler/SetHandler.ashx?action=Delete",
                   processData: true,//当发送字符串时设为true,当发送文档对象时设为false
                   data: {
                       LType: LType,
                       CardId: CardId,
                   },
                   async: false,
                   timeout: 5000
               });
               alert("提交成功！\n Submit Successfully !");
               window.location.reload();


           } else {
               return false;
           }
       }
	</script>

    <script type="text/javascript">
        var LType = "./Handler/SetHandler.ashx?action=LType";
        $.getJSON(LType, function (json) {
            $("#LType").combobox({
                data: json.rows,
                valueField: 'Id',
                textField: 'Name',
                required: 'true',
                editable: 'false'
            });
        });

        var Employee = "./Handler/SetHandler.ashx?action=Employee";
        $.getJSON(Employee, function (json) {
            $("#Employee").combobox({
                data: json.rows,
                valueField: 'Cardid',
                textField: 'Name',
                required: 'true',
                editable: 'true',
                onChange: function (newValue, oldValue) {
                    $("#signimg").attr("src", "showimg.aspx?ID=" + newValue); //回显图片
                }
            });
        });
    </script>

    <script type="text/javascript">
        $(function () {
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "./Handler/SetHandler.ashx?action=Winner",
                //data: "",
                success: function (data) {
                    createShowingTable(data);
                }
            });
        });

        //动态的创建一个table
        function createShowingTable(data) {
            var tableStr = "<table class='table table-bordered' width='99%'>";
            tableStr = tableStr
            + "<tr class='warning'>"
            + "<td class='text-center' width='8%'>序号</td>"
            + "<td class='text-center' width='12%'>奖项名称</td>"
            + "<td class='text-center' width='80%'>中奖名单(如果录入错误,点击头像删除！)</td>"
            + "</tr>";
            var len = data.length;
            for (var i = 0; i < len; i++) {

                tableStr = tableStr + "<tr>"
                + "<td class='active'>" + (i + 1) + "</td>"
                + "<td class='active'>" + data[i].LTypeName + "</td>"
                + "<td class='active'>  ";

                var strsc = new Array(); //定义一数组  
                var strse = new Array();
                strsc = data[i].CardId.split(","); //字符分割  
                strse = data[i].Employee.split(","); //字符分割  
                for (var j = 0; j < strsc.length ; j++) {
                    tableStr = tableStr
                        + " <div class='img-thumbnail' style='height:195px; float:left;border:1px solid silver; margin:2px 3px;'> "
                        + " <div > <img src='showimg.aspx?ID=" + strsc[j] + "' width='120' height='150' style='cursor:pointer' onclick='Delete(" + data[i].LType + ",\"" + strsc[j] + "\")'; ></div>"
                        + " <div style='width:120px; margin-top:2px ;font-size:12px'>" + strse[j] + "</div> </div>"
                }

                tableStr = tableStr + " </td>"

                + "</tr>";
            }
            if (len == 0) {
                tableStr = tableStr + "<tr style='text-align: center'>"
                + "<td colspan='3'><font color='#cd0a0a'>" + 暂无记录 + "</font></td>"
                + "</tr>";
            }
            tableStr = tableStr + "</table>";
            //添加到div中
            $("#tableAjax").html(tableStr);
        }
    </script>

    <script type="text/javascript">
        $.ajaxSetup({ cache: false }); //关闭AJAX相应的缓存,加上后IE浏览器重新刷新Datagrid不加载缓存
        //$.ajaxSettings.async = false; //getJSON异步进行，必须同步请求
    </script>

</head>
<body>
    <form id="form1" runat="server">

        <div style="margin-top: 10px"></div>

        <div style="width: 400px; margin: 10px 50px; text-align: left; background: white; padding: 10px; border-bottom-color:silver; float:left">

            <table id="dg" title="奖项设置" class="easyui-datagrid" style="width: 400px; height: auto; min-height: 210px"
                data-options="
			    iconCls: 'icon-edit',
			    singleSelect: true,
			    toolbar: '#tb',
                url: './Handler/SetHandler.ashx?action=Load',
			    method: 'get',
                rownumbers: true,
			    onClickRow: onClickRow ">
                <thead>
                    <tr>
                        <%--<th data-options="field:'Id',width:50,align:'center',editor:'textbox'">Id</th>--%>
                        <th data-options="field:'Name',width:120,align:'center',editor:'textbox'">奖项名称</th>
                        <th data-options="field:'Qty',width:60,align:'right',editor:'textbox'">中奖人数</th>
                        <th data-options="field:'Prize',width:120,align:'right',editor:'textbox'">奖品</th>
                        <th data-options="field:'IsVisable',width:60,align:'right',editor:{type:'checkbox',options:{on:'1',off:'0'}}">是否显示</th>
                    </tr>
                </thead>
            </table>

            <div id="tb" >
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">Append</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">Remove</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">Accept</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">Reject</a>
            </div>
        </div>

        <div style="width:400px; height:210px; margin: 20px 50px; text-align: left; background: white; padding: 10px; float:left; border:1px solid silver;">
            <table>
                <tr>
                    <td align="center" style="width:200px;""><img id="signimg" class="img-thumbnail" width="128" height="180" style="border:1px solid silver;" alt="" /></td>
                    <td align="center" style="width:200px; vertical-align:middle">
                        <div style="margin:20px;"><select id="LType" class="easyui-combobox" style="width:200px;" panelheight="auto"></select></div>
                        <div style="margin:20px;"><select id="Employee" class="easyui-combobox" style="width:200px;" panelheight="150px"></select></div>
                        <div style="margin-top:50px;"><input type="button" value="提交/Submit" class="btn btn-info" style="width:150px;" onclick="Submit()" /></div>
                    </td>
                </tr>

            </table>
        </div>

        <div id="tableAjax" style="width:910px; margin:10px 50px; text-align:left; background: white; padding: 10px; clear: both; border:1px solid silver;"> </div>

    </form>
</body>
</html>
